/**
 * 
 */
package org.gigi.security.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * 菜单
 *
 * @author Eugene Wang 2011-9-12
 * @see
 */
@Entity
@Table(name = "sec_menu")
public class Menu implements Serializable {
	private static final long serialVersionUID = 1L;

	//虚拟的根节点对应code值
	public static final String ROOT_CODE = "";

	@Id
	private String code;
	private String name;
	private String url;
	@Column(name="layer_order")
	private int layerOrder;
	@ManyToOne
	@JoinColumn(name = "parent_code")
	private Menu parent;
	@ManyToOne
	@JoinColumn(name = "authority")
	private Authority authority;
	//为了方便一次行全部加在所有菜单，用来存储子菜单，但是不负责维护菜单关系（性能原因）。
	@Transient
	private List<Menu> children = new ArrayList<Menu>();

	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = ("".equals(code) ? null : code);
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public int getLayerOrder() {
		return layerOrder;
	}
	public void setLayerOrder(int layerOrder) {
		this.layerOrder = layerOrder;
	}
	public Menu getParent() {
		return parent;
	}
	public void setParent(Menu parent) {
		this.parent = parent;
	}
	public Authority getAuthority() {
		return authority;
	}
	public void setAuthority(Authority authority) {
		this.authority = authority;
	}
	public List<Menu> getChildren() {
		return children;
	}
	/**
	 * 增加子节点
	 * @param child
	 * @return
	 */
	public boolean addChild(Menu child) {
		child.setParent(this);
		return children.add(child);
	}
}
